package org.hyperzsoft.one;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import org.hyperzsoft.Constants;
import org.hyperzsoft.Constants.*;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Team {
	int position;
	String fullName;
	String acroName;
	Division div;
	int gamesPlayed;
	int won;
	int lost;
	int otloss;
	int points;
	int regPlusOT;
	int goalsFor;
	int goalsAga;
	String homeRec;
	String awayRec;
	String lastTen;
	String streak;
	String soRec;
	float avgGFG;
	float avgGAG;
	int goalDiff;
	float winPerc;
	int lostPoints;
	float pointPerc;
	
	Map<String, OppTeam> gameSchedule;
	
	public Team() {
		gameSchedule = new HashMap<String, OppTeam>();
	}
	
	public void BuildSchedule(Element schedule) {
		OppTeam opTeam;
		Element opTeamData;
		Venues venue;
		
		for (Element gameRow : schedule.select("tr:has(td.team)")) {
			opTeam = new OppTeam();
			
			Elements teamsPlayed = gameRow.select("div.teamName").select("a");
			opTeamData = teamsPlayed.select("a:not(a[rel=" + this.acroName + "])").first();
			
			//if the first a element with a rel attr has a value that matches the current teams acronym name
			// then the current team was Away and the opposing team was Home and vice versa
			venue = (teamsPlayed.first().attr("rel").equals(this.acroName)) ? Venues.HOME : Venues.AWAY;
			
			opTeam.setFullName(opTeamData.text());
			opTeam.setAcroName(opTeamData.select("a").attr("rel"));
			
			if (gameSchedule.containsKey(opTeam.getAcroName())) {
				opTeam = gameSchedule.get(opTeam.getAcroName());
			}
			
			opTeam.addDate(gameRow.select("div.skedStartDateSite").text(), venue);
			gameSchedule.put(opTeam.getAcroName(), opTeam);
		}
	}
	
	public void BuildTeamFromNode(Element teamRow) {
		Elements teamInfo;
		DecimalFormat twoDForm = new DecimalFormat("#.##");
		
		teamInfo = teamRow.select("a:not(a:has(div))");
		acroName = teamInfo.attr("rel");
		fullName = teamInfo.text();
		
		teamInfo = teamRow.select("td").not("td[style]");
		position = Integer.parseInt(teamInfo.get(0).text());
		div = Division.valueOf(teamInfo.get(1).text());
		gamesPlayed = Integer.parseInt(teamInfo.get(2).text());
		won = Integer.parseInt(teamInfo.get(3).text());
		lost = Integer.parseInt(teamInfo.get(4).text());
		otloss = Integer.parseInt(teamInfo.get(5).text());
		points = Integer.parseInt(teamInfo.get(6).text());
		regPlusOT = Integer.parseInt(teamInfo.get(7).text());
		goalsFor = Integer.parseInt(teamInfo.get(8).text());
		goalsAga = Integer.parseInt(teamInfo.get(9).text());
		homeRec = teamInfo.get(11).text();
		awayRec = teamInfo.get(12).text();
		soRec = teamInfo.get(13).text();
		lastTen = teamInfo.get(14).text();
		streak = teamInfo.get(15).text();
		avgGFG = Float.valueOf(twoDForm.format((float)goalsFor / (float)gamesPlayed));
		avgGAG = Float.valueOf(twoDForm.format((float)goalsAga / (float)gamesPlayed));
		goalDiff = goalsFor - goalsAga;
		winPerc = Float.valueOf(twoDForm.format((float)won/(float)gamesPlayed));
		lostPoints = (gamesPlayed * 2) - ((won * 2) + otloss);
		pointPerc = Float.valueOf(twoDForm.format((float)(((float)won * 2) + (float)otloss) / (float)((float)gamesPlayed * 2)));
	}
}

class OppTeam {
	private String fullName;
	private String acroName;
	private Map<Long, Venues> gameDates;
	private Map<Long, HistoricalGame> hgGames;
	Division div;
	
	public OppTeam() {
		hgGames = new HashMap<Long, HistoricalGame>();
		gameDates = new HashMap<Long, Venues>();
	}
	
	public String getFullName() {
		return fullName;
	}
	
	public void setFullName(String newValue) {
		fullName = newValue;
	}
	
	public String getAcroName() {
		return acroName;
	}
	
	public void setAcroName(String newValue) {
		acroName = newValue;
	}
	
	public Map<Long, Venues> getDates() {
		return gameDates;
	}
	
	public void addDate(String value, Venues venue) {
		try {
			SimpleDateFormat format = new SimpleDateFormat(Constants.TIMESTAMP_FORMAT);
			gameDates.put(format.parse(value).getTime(), venue);
		} catch (ParseException e) {
			//Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void BuildOpponentHistory(Element history, String teamName) {
		HistoricalGame hgGame;
		SimpleDateFormat format = new SimpleDateFormat(Constants.TIMESTAMP_FORMAT);
		
		try {
			for (Element gameRow : history.select("tr:has(td.team)").select("tr:has(a[rel=" + teamName + "])")) {
				hgGame = new HistoricalGame();
				Elements teamsPlayed = gameRow.select("div.teamName").select("a");
				//find the venue for the opposing team
				hgGame.venue = (teamsPlayed.first().attr("rel").equals(this.acroName)) ? Venues.AWAY : Venues.HOME;  
				String winner = gameRow.select("td.tvInfo").select("span.black").text();
				String loser = gameRow.select("td.tvInfo").select("span:not(span.black)").text(); 
				int winGoals = Integer.parseInt(winner.substring(winner.indexOf("(") + 1, winner.indexOf(")"))); 
				int losGoals = Integer.parseInt(loser.substring(loser.indexOf("(") + 1, loser.indexOf(")")));
				
				hgGame.goalsFor = (winner.contains(this.getAcroName())) ? winGoals : losGoals; 
				hgGame.goalsAga = (winner.contains(this.getAcroName())) ? losGoals : winGoals;
				
				if (loser.contains("OT") || loser.contains("S/O") && loser.contains(this.getAcroName())) {
					hgGame.result = "OTL";
				} else {
					hgGame.result = (winner.contains(this.acroName)) ? "W" : "L";
				}
				
				hgGames.put((format.parse(gameRow.select("div.skedStartDateSite").first().text())).getTime(), hgGame);
			}
		} catch (ParseException e) {
			//Auto-generated catch block
			e.printStackTrace();
		}
	}
}

class HistoricalGame {
	Venues venue;
	int goalsFor;
	int goalsAga;
	String result;
}